package com.xlh.support.quiz.controller;

import com.xlh.common.Result;
import com.xlh.dto.quiz.EssayQuestionStaticsDTO;
import com.xlh.dto.quiz.TrueOrFalseQuestionStaticsDTO;
import com.xlh.support.quiz.po.QuizStatisticsQueryPO;
import com.xlh.support.quiz.service.QuizStatisticService;
import com.xlh.support.quiz.vo.QuizStatisticVO;
import com.xlh.vo.PageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author cheer
 */
@Api("随堂提问统计")
@RestController
@RequestMapping("/quiz/statistics/")
public class QuizStatisticsController {

    @Autowired
    private QuizStatisticService quizStatisticService;

    @ApiOperation("查询提问统计")
    @PostMapping("/{courseId}")
    @PreAuthorize("@permissionValidator.isTeacher()")
    public Result<PageVO<QuizStatisticVO>> listQuizStatistic(@PathVariable("courseId") Long courseId,
                                                             @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                                             @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                                             @RequestBody @Valid QuizStatisticsQueryPO quizStatisticsQueryPO) {
        List<QuizStatisticVO> quizStatisticVOs = quizStatisticService.listQuizStatistic(courseId, quizStatisticsQueryPO);

        PageVO<QuizStatisticVO> pageVO = PageVO.<QuizStatisticVO>builder()
                .pageNum(pageNum)
                .pageSize(pageSize)
                .total(quizStatisticVOs.size())
                .pages(quizStatisticVOs.size() == 0 ? 0 : quizStatisticVOs.size() / (pageSize + 1))
                .content(quizStatisticVOs.subList((pageNum - 1) * pageSize, Math.min(pageNum * pageSize, quizStatisticVOs.size())))
                .build();

        return Result.success(pageVO);
    }

    @ApiOperation("下载提问统计")
    @PostMapping("/{courseId}/download")
    @PreAuthorize("@permissionValidator.isTeacher()")
    public Result download(@PathVariable("courseId") Long courseId,
                           @RequestBody @Valid QuizStatisticsQueryPO quizStatisticsQueryPO) {
        quizStatisticService.downloadQuizStatistic(courseId, quizStatisticsQueryPO);
        return Result.success();
    }


    @ApiOperation("问答题统计")
    @GetMapping("/essayQuestion")
    @PreAuthorize("@permissionValidator.isTeacher()")
    public Result essayQuestion(@RequestParam(value = "chapterId", required = true) Long chapterId ,
                                @RequestParam(value = "defaultClassId", required = true) Long defaultClassId) {
        EssayQuestionStaticsDTO essayQuestionStaticsDTO = quizStatisticService.essayQuestionStatistic(chapterId, defaultClassId);
        return Result.success(essayQuestionStaticsDTO);
    }

    @ApiOperation("判断题统计")
    @GetMapping("/trueOrFalseQuestions")
    @PreAuthorize("@permissionValidator.isTeacher()")
    public Result trueOrFalseQuestions ( Long chapterId , Long defaultClassId) {
        TrueOrFalseQuestionStaticsDTO trueOrFalseQuestionStaticsDTO = quizStatisticService.trueOrFalseQuestionsStatistic(defaultClassId, chapterId);
        return Result.success(trueOrFalseQuestionStaticsDTO);
    }



}
